package com.xiaozhaovip.demo.controller.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ClassUtils;
import sun.misc.BASE64Decoder;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Cauli
 */
@Slf4j
public class Base64ToImageUtil {
    public static String uploadBase64Pic(String content, String realPath) {
        log.info("real path =:" + realPath);
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        try {
            BufferedOutputStream bos;
            FileOutputStream fos;

            String pattern = "data:image[a-zA-Z0-9\\pP\\+\\-\\=]+";
            Pattern p = Pattern.compile(pattern);

            Matcher matcher = p.matcher(content);
            while (matcher.find()) {
                String pic64 = matcher.group(0);
                pic64 = pic64.replaceAll("\"", "");
                String fileName;
                String base64;
                if (pic64.contains("jpeg")) {
                    fileName = UUIDUtil.getUUID() + ".jpeg";
                    base64 = pic64.substring(23);
                } else {
                    fileName = UUIDUtil.getUUID() + ".png";
                    base64 = pic64.substring(22);
                }

                byte[] bytes = new BASE64Decoder().decodeBuffer(base64);

                for (int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {
                        bytes[i] += 256;
                    }
                }

                File file = new File(realPath + "/" + fileName);
                fos = new FileOutputStream(realPath + "/" + fileName);
                fos.write(bytes);

                if (!file.exists()) {
                    log.info("file is not exist");
                }

                log.info("filename = :" + fileName);
                log.info("file length" + file.length() + " _ file:" + file.getAbsolutePath());
//				OSSUtils.uploadInnerFile(file,OSSConstant.RDAXUE_BUCKET, "pro/image/" + fileName);
//				content = content.replace(pic64,OSSConstant.IMAGE_BUCKET_URL + fileName + "?x-oss-process=image/resize,w_500");

                fos.flush();
                fos.close();
                fos = null;
                // 本地时不删除
                if (file.exists()) {
                    file.delete();
                }

                return fileName;
            }
        } catch (IOException e) {
            log.error("upload article content pic fail", e);
        }
        return null;
    }

    public static String uploadBase64Img(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        try {
            FileOutputStream fos;

            String imagePath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/upload/images/";
            // 通过base64来转化图片
            byte[] bytes = new BASE64Decoder().decodeBuffer(content);
            for (int i = 0; i < bytes.length; ++i) {
                // 调整异常数据
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            String fileName = UUIDUtil.getUUID() + ".png";
            // 创建目录
            File pathFile = new File(imagePath);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            // 创建文件
            File file = new File(imagePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.flush();

            log.info("filename = :" + fileName);

            fos.flush();
            fos.close();

            return "/upload/images/" + fileName;
        } catch (IOException e) {
            log.error("upload article content pic fail", e);
        }
        return null;
    }

}
